class ActiveResource::JsonPaginating < ActiveResource::Base  # ActiveRecord::Base

  def self.find(*args)
    default_format = self.format
    self.format = self.connection.format = ActiveResource::Formats[:json_paginating]
    super(*args)
  ensure
    self.format = self.connection.format = default_format
  end

  def self.find_every(options)
    options = options.dup
    options[:params] ||= {}
    options[:params][:page] = 1 unless options[:params][:page].to_i > 0
    options[:params][:limit] = 10 unless options[:params][:limit].to_i > 0
    records = super(options)
    eval %Q{
      def records.page
        #{options[:params][:page]}
      end
      def records.per_page
        #{options[:params][:limit]}
      end
      def records.total_page
        total_page = total / per_page
        total_page = total_page + 1 if total_page * per_page < total
        total_page
      end
    }
    records
  end

end

